var wxCharts = require('../../../utils/wx_charts.js')    // 根据wxCharts所在的位置而定
var Function = require("../../../utils/function.js");
import request from '../../../utils/request.js'
var yuelineChart = ''
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0 ,//索引
    date_choose: 'all',
    detail : {}  //收益详情
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  getDetail(){  //获取收益详情
    var token = wx.getStorageSync('token') || '';
    if (token == '') {
      wx.showModal({
        title: '提示',
        content: '为了保证您的信息安全，请先授权登录！',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/authorization/auth/auth',
            })
          }
        }
      })
    } else {
      request({
        url: '/api/jiameng.index/getPerformance',
        data: {
          token: token,
          date : this.data.date_choose
        },
        header: {
          token: token
        }
      }).then(res => {
        console.log(res, '收益')
        if (res.data.code == 200) {
          this.setData({
            detail : res.data.data
          })
          this.getMothElectro(); //加载饼状图
        } else if(res.data.code == 401){
          Function.layer(res.data.msg + '！');
          setTimeout(()=>{
           wx.navigateTo({
             url: '/pages/authorization/auth/auth',
           })
          },1500)
       }
        else {
          Function.layer('获取收益失败，请稍后重试或联系工作人员！')
        }
      })
    }
  },
  handChooseType(e) {  //点击tap切换
    if (this.data.activeIndex == e.currentTarget.dataset.index) {
      return false;
    }
    this.setData({
      activeIndex: e.currentTarget.dataset.index
    })
    if(e.currentTarget.dataset.index == 0){
     this.setData({
       date_choose : 'all'
     })
    }else if(e.currentTarget.dataset.index == 1){
      this.setData({
        date_choose : 'day'
      })
    }else if (e.currentTarget.dataset.index == 2){
      this.setData({
        date_choose : 'month'
      })
    }
 this.getDetail();//加载接口
  },
  createSimulationData() { //获取饼图动态值
    var services_name = this.data.detail.percent_statistics;
    return {
      services_name : services_name
    }
  },
  // 展示方法（饼图）
  getMothElectro: function () {
    var simulationData1 = this.createSimulationData();
    yuelineChart = new wxCharts({
      animation: true,      //是否有动画
      canvasId: 'pieCanvas',
      type: 'pie',
      series: [{
        name: simulationData1.services_name[0].name,
        data: simulationData1.services_name[0].value,
        color: '#156df2'
      }
      ],
      width: 310, // 宽度，单位为px
      height: 300, // 高度，单位为px
      dataLabel: true,
      legend: true
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  this.getDetail();
  }

})